package cn.linjianhui.article.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Getter
@Setter
public class Article {
    private Long id;
    private String title;
    private String context;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime releaseTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    public String getContext() {
        if(StringUtils.hasText(context)) {
            String noHtml = context.replaceAll("<.+?>", "");
            if(noHtml.length() > 0) {
                return noHtml.substring(0, Math.min(noHtml.length(), 50)) + "...";
            }
        }
        return "";
    }
}
